package com.buka.service.impl;

import com.buka.http.AppInfo;
import com.buka.mapper.UserMapper;
import com.buka.pojo.User;
import com.buka.service.UserService;
import com.buka.utils.ResponseJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;

/**
 * UserServiceImpl 实现类
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 用户端注册
     */
    public ResponseJson regis(String username, String password) {
        User user = new User(username, password);
        int i = userMapper.add(user);
        return i > 0 ? ResponseJson.getOK() : ResponseJson.getError();
    }

    /**
     * 用户端登录
     */
    public ResponseJson login(String username, String password, HttpSession httpSession) {
        User user = userMapper.getUserByUsername(username);
        if (user != null) {
            if (user.getUserPassword().equals(password)) {
                httpSession.setAttribute(AppInfo.SESSION_USER, user);
                return ResponseJson.getOK();
            } else {
                return ResponseJson.getError("密码错误");
            }
        }
        return ResponseJson.getError("用户不存在");
    }

}
